package com.ruoyi.project.medicinal.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.medicinal.domain.StockProducter;
import com.ruoyi.project.medicinal.domain.StockProvider;
import com.ruoyi.project.medicinal.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/med/prov")
public class ProviderController extends BaseController {

    @Autowired
    private ProviderService providerService;

    /**
     * 获取供应商列表
     */
    @PreAuthorize("@ss.hasPermi('med:prov:list')")
    @GetMapping("/list")
    public TableDataInfo list(StockProvider provider) {
        startPage();
        List<StockProvider> list = providerService.selectProvList(provider);
        return getDataTable(list);
    }

    /**
     * 新增供应商
     */
    @PreAuthorize("@ss.hasPermi('med:prov:add')")
    @Log(title = "供应商管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody StockProvider provider) {
        provider.setCreateBy(SecurityUtils.getUsername());
        return toAjax(providerService.insertProv(provider));
    }

    /**
     * 根据供应商编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('med:prov:query')")
    @GetMapping(value = "/{providerId}")
    public AjaxResult getInfo(@PathVariable Long providerId) {
        return AjaxResult.success(providerService.selectProvById(providerId));
    }

    /**
     * 修改供应商
     */
    @PreAuthorize("@ss.hasPermi('med:prov:edit')")
    @Log(title = "供应商管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody StockProvider provider)
    {
        provider.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(providerService.updateProv(provider));
    }

    /**
     * 批量删除供应商
     */
    @PreAuthorize("@ss.hasPermi('med:prov:remove')")
    @Log(title = "供应商管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{providerId}")
    public AjaxResult remove(@PathVariable Long[] providerId) {
        return toAjax(providerService.deleteProvByIds(providerId));
    }
}
